﻿#region LGPL
/************************************************************************************/
/* Fireball.Windows.Forms a controls library for windows forms
 * 
 * Copyright (C) 2009 Sebastian Faltoni
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the 
 * Free Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
 * for more details.
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, write to the Free Software Foundation, Inc., 59 
 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 * Signature of Sebastian Faltoni, 2 Febbraio 2009
 * Sebastian Faltoni, President of Orangebit
 * 
 ***********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using Orangebit.Windows.Forms.Properties;

namespace Orangebit.Windows.Forms
{
    /// <summary>
    /// A Default theme for the explorerBar control that mimic , the windows xp style
    /// </summary>
    public class DefaultExplorerBarTheme : IExplorerBarTheme
    {
        private Color BackgroundColor1;
        private Color BackgroundColor2;
        private LinearGradientBrush GradientBrush = null;
        private Color ExplorerBarItemHeaderColor1;
        private  Color ExplorerBarItemHeaderColor2;
        private  LinearGradientBrush ExplorerBarItemHeaderBrush = null;
        private  GraphicsPath ExplorerBarItemHeaderPath = null;
        private  Color ExplorerBarItemContentBackground;
        private  Bitmap CollapseButton;
        private  Bitmap ExpandButton;
        private  Font HeaderFont = null;
        private  SolidBrush HeaderTextBrush = null;
        private  StringFormat HeaderTextStringFormat = null;

        public DefaultExplorerBarTheme()
        {
            BackgroundColor1 = Color.FromArgb(255, 123, 162, 231);
            BackgroundColor2 = Color.FromArgb(255, 99, 117, 214);
            ExplorerBarItemHeaderColor1 = Color.White;
            ExplorerBarItemHeaderColor2 = Color.FromArgb(255, 199, 212, 247);
            ExplorerBarItemContentBackground = Color.FromArgb(255, 214, 223, 247);
            Color tmp = Properties.Resources.collapse.GetPixel(0, 0);
            CollapseButton = Properties.Resources.collapse;
            CollapseButton.MakeTransparent(tmp);

            tmp = Properties.Resources.collapse.GetPixel(0, 0);
            ExpandButton = Properties.Resources.expand;
            ExpandButton.MakeTransparent(tmp);

            HeaderFont = new Font("Tahoma", 11, FontStyle.Bold, GraphicsUnit.Pixel);
            HeaderTextBrush = new SolidBrush(Color.SteelBlue);

            HeaderTextStringFormat = new StringFormat();
            HeaderTextStringFormat.LineAlignment = StringAlignment.Center;
            HeaderTextStringFormat.Trimming = StringTrimming.EllipsisCharacter;
            HeaderTextStringFormat.FormatFlags = StringFormatFlags.NoWrap;
        }

        #region IExplorerBarTheme Members

        public void PaintExplorerItem(ExplorerBarItem item, System.Windows.Forms.PaintEventArgs evt)
        {
            if (this.ExplorerBarItemHeaderBrush == null ||
                this.ExplorerBarItemHeaderPath == null)
                return;
            // evt.Graphics.FillRectangle(new SolidBrush(Color.Transparent), new Rectangle(0, 0, item.Width, item.Height));
            evt.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            int headerH = (item.HeaderImage == null ? 24 : 40);
            int top = (item.HeaderImage == null ? 0 : 16);
            int contentPadd = 1;
            evt.Graphics.FillPath(ExplorerBarItemHeaderBrush, ExplorerBarItemHeaderPath);
            evt.Graphics.FillRectangle(new SolidBrush(Color.White), new Rectangle(0, headerH, item.Width, item.Height - headerH));
            evt.Graphics.FillRectangle(new SolidBrush(ExplorerBarItemContentBackground),
                new Rectangle(contentPadd, headerH, item.Width - contentPadd * 2, item.Height - headerH));
            if (item.HeaderImage != null)
                evt.Graphics.DrawImage(item.HeaderImage, new Rectangle(5, 0, 32, 32));

            //if (item.IsExpanded && !item.IsMouseOverHeader)
            //{
            //    evt.Graphics.DrawImage(Properties.Resources.NORMALGROUPCOLLAPSE, this.GetToogleButtonArea(item));
            //}
            //else if (item.IsExpanded && item.IsMouseOverHeader)
            //{
            //    evt.Graphics.DrawImage(Properties.Resources.NORMALGROUPCOLLAPSEHOT, this.GetToogleButtonArea(item));
            //}
            //else if (!item.IsExpanded && !item.IsMouseOverHeader)
            //{
            //    evt.Graphics.DrawImage(Properties.Resources.NORMALGROUPEXPAND, this.GetToogleButtonArea(item));
            //}
            //else if (!item.IsExpanded && item.IsMouseOverHeader)
            //{
            //    evt.Graphics.DrawImage(Properties.Resources.NORMALGROUPEXPAND, this.GetToogleButtonArea(item));
            //}
            if (item.IsExpanded)
            {
                evt.Graphics.DrawImage(CollapseButton, this.GetToogleButtonArea(item));
            }
            else if (!item.IsExpanded)
            {
                evt.Graphics.DrawImage(ExpandButton, this.GetToogleButtonArea(item));
            }

            evt.Graphics.DrawString(item.HeaderText, HeaderFont, HeaderTextBrush, GetHeaderTextArea(item), HeaderTextStringFormat);
        }

        public void PaintExplorerBar(ExplorerBar explorerBar, System.Windows.Forms.PaintEventArgs evt)
        {
            evt.Graphics.FillRectangle(GradientBrush, new Rectangle(0, 0, explorerBar.Width, explorerBar.Height));
        }

        public void OnExplorerBarItemSizeChanged(ExplorerBarItem item)
        {
            if (item.Width == 0 || item.Height == 0)
                return;
            int topPadd = (item.HeaderImage == null ? 24 : 40);
            int top = (item.HeaderImage == null ? 0 : 16);

            item.Padding = new System.Windows.Forms.Padding(10, topPadd + 11, 10, 10);
            item.Refresh();

            ExplorerBarItemHeaderBrush = new LinearGradientBrush(new Rectangle(0, 0, item.Width, topPadd), ExplorerBarItemHeaderColor1, ExplorerBarItemHeaderColor2, 45);

            ExplorerBarItemHeaderPath = new GraphicsPath();

            int archConst = 8;
            Rectangle leftArc = new Rectangle(0, top, archConst, archConst);
            Rectangle rightArc = new Rectangle(item.Width - archConst - 1, top, archConst, archConst);

            ExplorerBarItemHeaderPath.StartFigure();
            ExplorerBarItemHeaderPath.AddArc(leftArc, 180, 90);

            ExplorerBarItemHeaderPath.AddLine(16, top, item.Width - (archConst * 2), top);
            ExplorerBarItemHeaderPath.AddArc(rightArc, 270, 90);
            ExplorerBarItemHeaderPath.AddLine(item.Width, top + archConst, item.Width, topPadd);
            ExplorerBarItemHeaderPath.AddLine(item.Width, topPadd, 0, topPadd);
            ExplorerBarItemHeaderPath.AddLine(0, topPadd, 0, top + archConst);
            ExplorerBarItemHeaderPath.CloseFigure();

        }

        public int GetCollapsedHeight(ExplorerBarItem item)
        {
            return item.HeaderImage == null ? 24 : 40;
        }

        public void OnExplorerBarSizeChanged(ExplorerBar explorerBar)
        {
            if (explorerBar.Height > 0 && explorerBar.Width > 0)
                GradientBrush = new LinearGradientBrush(new Rectangle(0, 0, explorerBar.Width, explorerBar.Height), BackgroundColor1, BackgroundColor2, 90);
        }

        public Rectangle GetToogleButtonArea(ExplorerBarItem item)
        {
            int topPadd = (item.HeaderImage == null ? 24 : 40);
            int top = (item.HeaderImage == null ? 0 : 16);

            int btTop = top + (12 - 10);

            return new Rectangle(item.Width - 3 - 20, btTop, 20, 20);
        }


        public Rectangle GetHeaderTextArea(ExplorerBarItem item)
        {
            int top = (item.HeaderImage == null ? 0 : 16) + 12 - 6;
            int left = (item.HeaderImage == null ? 10 : 42);
            return new Rectangle(left, top, item.Width - 26/* 26 e'  lo spazio ris. al bottone*/,
                    11
                );
        }

        #endregion
    }
}
